package de.tabuma.shopping.view;

import de.tabuma.shopping.entity.*;
import de.tabuma.shopping.logik.*;
import de.tabuma.lib.*;
import de.tabuma.shopping.*;

import java.io.*;
import java.util.*;
import java.util.logging.*;
import java.text.*;

import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.4.1 $
 * @since $Date: 2010/01/13 10:22:52 $
 */
public class licensePackageOverview extends tabumaview {

    private static Logger log =  Logger.getLogger(licensePackageOverview.class.getName());

    String orderNav;

    de.tabuma.shopping.controller.controller controller;

    public licensePackageOverview(String servlet, Hashtable parameter, String orderNav, de.tabuma.shopping.controller.controller controller) {
        super(servlet, parameter);
        this.orderNav = orderNav;
        this.controller = controller;
    }

    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out)  {
        try {
            out.println("<div class=\"contentContent\">");
            out.println("<p class=\"contentContentHeader\">" + constants.getValueFromProperties("orders.licensepackage.head") + "</p>");
            out.println(constants.getValueFromProperties("orders.licensepackage.content")+"<br/><br/>");

            out.println(this.getFormBeginn(null));
            out.println(this.getHiddenAsAttribute());
            out.println("<input type=\"hidden\" name=\""+orderNav+"\" value=\""+de.tabuma.shopping.constants.ORDERNAV_PACKAGE+"\"/>");

            List<LicensePackage> licensesPackages = (List<LicensePackage>) request.getAttribute(de.tabuma.shopping.constants.ORDER_CURRENT_LICENSEPACKAGE_LIST);
            List<LicensePackage> licensesPackagesAll = (List<LicensePackage>) request.getAttribute(de.tabuma.shopping.constants.ORDER_ALL_LICENSEPACKAGE_LIST);
            
            int i = 0;
            NumberFormat nf = NumberFormat.getCurrencyInstance();
            nf.setCurrency(Currency.getInstance(Locale.GERMANY));
            String price = constants.getValueFromProperties("orders.licensepackage.price");
            String package1 = constants.getValueFromProperties("orders.licensepackage.offeringlist");
            out.println("<div class=\"OrderLicensePackageAll\">");

            //the left / offering side follows
            out.println("<div class=\"OrderLicensePackageAll1\">");
            out.println("\t<div class=\"OrderLicensePackageHead\">");
            out.println("\t\t<div class=\"orderLicensePackageHeadContent\">"+package1+"</div>");
            out.println("\t\t<div class=\"OrderLicensePackageRightHead\">");
            out.println("\t\t\t<div class=\"OrderLicensePackagePriceHead\">"+price+"</div>");
            out.println("\t\t\t<div class=\"OrderLicensePackageShoppingCardHead\"></div>");
            out.println("\t\t</div>");
            out.println("\t</div>");
            if(licensesPackages!=null) {
                Iterator<LicensePackage> licenseIterator = licensesPackages.iterator();
                while(licenseIterator.hasNext()) {
                    i++;
                    LicensePackage lp = licenseIterator.next();
                    String name = controller.getValueFromProperties("orders.licensepackage.package"+lp.getId()+".name");
                    String description = controller.getValueFromProperties("orders.licensepackage.package"+lp.getId()+".description");
                    if(request.getAttribute("plusLP"+lp.getId())!=null) {
                        out.println("\t<div class=\"OrderLicensePackage"+(i%2)+"\">");
                        out.println("\t\t<div class=\"OrderLicensePackageContent\">");
                        out.println("\t\t\t<div class=\"OrderLicensePackageContentLeft\"><input type=\"image\" src=\"grafiken/minus.png\" alt=\"minus view\" name=\"minusLP\" value=\"true\"/></div>");
                        out.println("\t\t\t<div class=\"OrderLicensePackageContentRight\">");
                        out.println("\t\t\t\t<div class=\"OrderLicensePackageContentPackage\">"+name+"</div>");
                        out.println("\t\t\t\t<div class=\"OrderLicensePackageContentPrice\">"+nf.format(lp.getPrice())+"*</div>");
                        out.println("\t\t\t\t<div class=\"OrderLicensePackageContentDescription\">"+description+"</div>");
                        out.println("\t\t\t\t<div class=\"OrderLicensePackageButton\"><input type=\"image\"  src=\"grafiken/shopping_cart.png\" name=\"LicensePackage"+lp.getId()+"\" value=\""+constants.getValueFromProperties("orders.licensepackage.button.add")+"\"></div>");
                        out.println("\t\t\t</div>");
                        out.println("\t\t</div>");
                        out.println("\t</div>");
                    }
                    else {
                        out.println("\t<div class=\"OrderLicensePackage"+(i%2)+"_small\">");
                        out.println("\t\t\t<div class=\"OrderLicensePackageContentLeft\"><input type=\"image\" src=\"grafiken/plus.png\" alt=\"plus view\" name=\"plusLP"+lp.getId()+"\" value=\"plusLP"+lp.getId()+"\"/></div>");
                        out.println("\t\t<div class=\"OrderLicensePackageContentRight\">");
                        out.println("\t\t\t<div class=\"OrderLicensePackageContentPackage\">"+name+"</div>");
                        out.println("\t\t\t<div class=\"OrderLicensePackageContentRight1\">");
                        out.println("\t\t\t\t<div class=\"OrderLicensePackageContentPrice\">"+nf.format(lp.getPrice())+"*</div>");
                        out.println("\t\t\t\t<div class=\"OrderLicensePackageButton\"><input type=\"image\" src=\"grafiken/shopping_cart.png\" name=\"LicensePackage"+lp.getId()+"\" value=\""+constants.getValueFromProperties("orders.licensepackage.button.add")+"\"></div>");
                        out.println("\t\t\t</div>");
                        out.println("\t\t</div>");
                        out.println("\t</div>");
                    }
                }
            }
            out.println("\t<div class=\"OrderLicensePackageEmpty\"></div>");
            out.println("\t</div>");

            //the right / ordering side follows
            out.println("\t<div class=\"OrderLicensePackageAll2\">");

            out.println("\t\t<div class=\"OrderListHead\">");
            out.println("\t\t\t<div class=\"OrderListHeadPackagename\">"+constants.getValueFromProperties("orders.licensepackage.orderlist.header")+"</div>");
            out.println("\t\t\t<div class=\"OrderListHeadRight\">");
            out.println("\t\t\t\t<div class=\"OrderListHeadPrice\">"+price+"</div>");
            out.println("\t\t\t\t<div class=\"OrderListHeadLitterBasket\"><input type=\"image\" src=\"grafiken/litterbasket.png\" alt=\"Remove All\" name=\"removeOrderAll\" value=\"true\"/></div>");
            out.println("\t\t\t</div>");
            out.println("\t\t</div>");

            out.println("\t\t<div class=\"OrderListList\">");
            CurrentOrder currentOrder = (CurrentOrder) request.getAttribute(constants.ORDER_CURRENT);
            List<CurrentOrderLicensePackageRelation> colprs = (List<CurrentOrderLicensePackageRelation>) request.getAttribute(constants.ORDER_CURRENT_LICENSEPACKAGERELATION_LIST);
            double sumPrice = 0;
            boolean in = false;
            if(currentOrder!=null && colprs!=null) {
                for(int j=0;j<colprs.size();j++) {
                    in = true;
                    Integer int1 = colprs.get(j).getLicensePackageId();
                    LicensePackage lp = null;
                    for(int k=0;k<licensesPackagesAll.size();k++) {
                        if(licensesPackagesAll.get(k).getId().intValue()==int1) {
                            lp = licensesPackagesAll.get(k);
                        }
                    }
                    String name = controller.getValueFromProperties("orders.licensepackage.package"+lp.getId()+".name");
                    String description = controller.getValueFromProperties("orders.licensepackage.package"+lp.getId()+".description");
                    if(request.getAttribute("plusOrderElement"+i)==null) {
                        out.println("\t\t\t<div class=\"OrderListContentRow"+j%2+"_small\">");
                        out.println("\t\t\t\t<div class=\"OrderListContentLeftSide\">");
                        out.println("\t\t\t\t<div class=\"OrderListContentPlus\"><input type=\"image\" src=\"grafiken/plus.png\" alt=\"Show details\" name=\"plusOrderElement"+j+"\"/></div>");
                        out.println("\t\t\t\t<div class=\"OrderListContentPackagename\">"+name+"</div>");
                        out.println("\t\t\t</div>");
                        out.println("\t\t\t\t<div class=\"OrderListContentRightSide\">");
                        out.println("\t\t\t\t\t<div class=\"OrderListContentPrice\">"+nf.format(lp.getPrice())+"*</div>");
                        out.println("\t\t\t\t\t<div class=\"OrderListContentRemove\"><input type=\"image\" src=\"grafiken/remove.png\" alt=\"Remove\" name=\"removeOrderID"+j+"\" value=\"true\"/></div>");
                        out.println("\t\t\t\t</div>");
                        out.println("\t\t\t</div>");
                    }
                    else {
                        out.println("\t\t\t<div class=\"OrderListContentRow"+j%2+"\">");
                        out.println("\t\t\t\t<div class=\"OrderListContentLeftSide\">");
                        out.println("\t\t\t\t<div class=\"OrderListContentPlus\"><input type=\"image\" src=\"grafiken/minus.png\" alt=\"Show details\" name=\"minusOrderElement\"/></div>");
                        out.println("\t\t\t\t\t<div class=\"OrderListContentPackagename\">"+name+"</div>");
                        out.println("\t\t\t</div>");
                        out.println("\t\t\t\t<div class=\"OrderListContentRightSide\">");
                        out.println("\t\t\t\t\t<div class=\"OrderListContentPrice\">"+nf.format(lp.getPrice())+"*</div>");
                        out.println("\t\t\t\t\t<div class=\"OrderListContentRemove\"><input type=\"image\"  src=\"grafiken/remove.png\" alt=\"Remove\" name=\"removeOrderID"+j+"\" value=\"true\"/></div>");
                        out.println("\t\t\t\t</div>");
                        out.println("\t\t\t\t\t<div class=\"OrderListContentDescription\">"+description+"</div>");
                        out.println("\t\t\t</div>");
                    }
                    sumPrice+=lp.getPrice();
                }
            }
            if(!in) {
                out.println("\t\t\t<div class=\"OrderListContentRow0\">");
                out.println("\t\t\t<div class=\"OrderListContentPackagename\">"+constants.getValueFromProperties("orders.licensepackage.orderlist.nolistentry")+"</div>");
                out.println("\t\t\t\t<div class=\"OrderListContentRightSide\">");
                out.println("\t\t\t\t<div class=\"OrderListContentPrice\"></div>");
                out.println("\t\t\t\t<div class=\"OrderListContentRemove\"></div>");
                out.println("\t\t\t</div>");
                out.println("\t\t\t</div>");

            }

            out.println("\t\t\t<div class=\"OrderListSumHead\">"+constants.getValueFromProperties("orders.licensepackage.orderlist.total")+"</div>");
            out.println("\t\t\t\t<div class=\"OrderListSumContent\">"+nf.format(sumPrice)+"</div>");
            out.println("\t\t\t\t<div class=\"OrderListSumTaxHead\">"+constants.getValueFromProperties("orders.licensepackage.orderlist.vat")+"</div>");
            out.println("\t\t\t\t<div class=\"OrderListSumTaxContent\">"+nf.format(sumPrice/(1+constants.vat/100)*constants.vat/100.0)+"</div>");
            out.println("\t\t\t</div>");

            if(in) {
                out.println("\t\t\t<div class=\"OrderListStartOrderButton\"><input type=\"submit\" name=\"startorder\" value=\""+constants.getValueFromProperties("orders.licensepackage.button.startorder")+"\" class=\"loginbutton\"/></div>");
            }
            out.println("\t\t<div class=\"OrderListTaxHint\">* "+constants.getValueFromProperties("orders.licensepackage.hint")+"</div>");
            out.println("\t<div class=\"OrderLicensePackageEmpty\"></div>");
            out.println("\t</div>");
            out.println("</div>");
            out.println("</form>");            
            out.println("</div>");
        } 
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i] + "\n";
            }
            log.warning(warning);
        }
    }
}